<?php 
class jobsAction extends AdminMaster{
	public function preExecute(){
		$this->formFilter['filter_company_id'] = '-1';
		$this->formFilter['filter_position_id'] = '-1';
		$this->formFilter['filter_location_city_id'] = '-1';
		$this->formFilter['filter_age_from'] = '-1';
		$this->formFilter['filter_age_to'] = '-1';
		$this->formFilter['filter_is_recruit'] = '1';
		$this->formFilter['filter_date_from'] = date('d-m-Y', time());
		//$this->formFilter['filter_search_method_id'] = '0';
		$this->formFilter['filter_gender_id'] = '-2';
		$this->formFilter['filter_salary_from'] = '0';
		//$this->formFilter['filter_salary_to'] = '0';
		
		$this->formFilter['filter_date_to'] = '';
		$this->formFilter['filter_major_ids'] = array();
		$this->formFilter['filter_education_level_ids'] = array();
		$this->formFilter['filter_location_city_ids'] = array();
		
		
		$this->data['filter_level_id']				= MasterEducationLevelsTable::getDataList(null);
		$this->data['filter_major_id']				= MasterMajorCatesTable::getDataRecursive();
		
		$this->data['filter_company_id']		= CompaniesTable::getDataList(array('filter_enabled'=>'1'));
		$this->data['filter_position_id']		= CompanyPersonContactPositionsTable::getDataList(array('filter_is_enabled'=>'1'));
		$this->data['filter_location_city_id']		= LocationCitiesTable::getDataList();
		parent::preExecute();
	}
	public function execute($request){
		$this->data['navigation']	= __('module_companies',null,'menu').' › '. __('module_companies_action_jobs',null,'menu');
		$this->formData				= $request->getParameter('Jobs');	//get form data from add new || edit page
		
		$this->data['rdSalary']		= $request->getParameter('rdSalary', '3');
		$this->data['majors']= $request->getParameter('majors', array());
		$this->data['cities']= $request->getParameter('cities', array());
		$this->data['levels']= $request->getParameter('levels', array());
		
		$this->data['child']['languages']		= $request->getParameter('languages', array());
		$this->data['child']['language_des']	= $request->getParameter('language_des', array());
		$this->data['child']['language_list']	= MasterForeignLanguages::getArrayAllKeyId();
		$this->data['child']['language_level_list']	= MasterForeignLanguageLevels::getArrayAllKeyId();
		
		//$this->data['list']['cates'] = ;
		
		switch ($this->data['action']){
			case ADMIN_DEFINED::DELETE_RECORD:
				$this->delRecord();
				break;
			case ADMIN_DEFINED::DISABLE:
				$this->disableRecord();
				break;
			case ADMIN_DEFINED::ENABLE:
				$this->enableRecord();
				break;
			case ADMIN_DEFINED::ADD_RECORD:
				//$this->redirect('/admin/vi/faqs/cates');die;
				$this->addRecord();
				break;
			case ADMIN_DEFINED::EDIT_RECORD:
				//$this->redirect('/admin/vi/faqs/cates');die;
				$this->editRecord();
				break;
			case ADMIN_DEFINED::SAVE_ORDER:
				$this->saveOrder();
				break;
			#MORE ACTION HERE
			default:
				$this->filterList();
				break;
		}

		$this->setVar('data', $this->data);
	}
	private function filterList(){
		$this->setAction();
		$this->data['navigation'].=' › '. __('nav_list',null,'table_list');
		
		$this->data['list']['jobs']			 		= JobsTable::getDataList($this->data['filter'], $this->data['pagination']);
		
		$this->data['pagination']['total']		= JobsTable::getDataListCount($this->data['filter']);
		$this->data['pagination']['router']		= 'company_jobs';
		$this->data['pagination']['params']		= $this->data['filter'];
		$pag = new PaginationHelper2($this->data['pagination']);
		$this->data['pagination_pager']	= $pag->getPaginations();
		if($this->data['filter']['filter_key'] == '*'){
			$this->data['filter']['filter_key'] = '';
		}
	}
	private function delRecord(){
		JobsTable::delete($this->data['select_list']);
		$this->filterList();
	}
	private function disableRecord(){
		JobsTable::disable($this->data['select_list']);
		$this->filterList();
	}
	private function enableRecord(){
		JobsTable::enable($this->data['select_list']);
		$this->filterList();
	}
	private function saveOrder(){
		$sortData = array();
		for($i = 0; $i< count($this->data['ids']); $i++){
			$sortData[$this->data['ids'][$i]] = $this->data['sort_orders'][$i];
		}
		Jobs::saveSortOrder($sortData);
		$this->filterList();
	}
	/*********************************************************************************
	*
	* ADD NEW
	* 
	**********************************************************************************/
	private function addRecord(){
		$this->data['navigation'].=' › '. __('nav_add_new',null,'table_list');
		$this->data['button_action'] = __('button_add',null,'table_list');
		$this->setTemplate('jobsForm');
		
		$this->data['list']['majors']		= MasterMajorCatesTable::getDataRecursive();
		$this->data['list']['cities']		= LocationCitiesTable::getDataList();
		$this->data['list']['levels']		= MasterEducationLevelsTable::getDataList();
		
		
		$form = new JobsFForm();
		if($this->data['is_post']){
			if(intval($this->data['rdSalary']) == 1){
				$this->formData['salary_from'] = '0';
				$this->formData['salary_to'] = '0';
			}elseif(intval($this->data['rdSalary']) == 2){
				$this->formData['salary_payed'] = '0';
			}else {
				$this->formData['salary_from'] = '0';
				$this->formData['salary_to'] = '0';
				$this->formData['salary_payed'] = '0';
			}
			$form->bind($this->formData);
			if($form->isValid()){
				$job_id = JobsTable::insert($this->formData);
				if($job_id > 0){
					
					$this->addMajor($job_id);
					$this->addCity($job_id);
					$this->addLevel($job_id);
					$this->addLanguage($job_id);
					
					$this->data['result']	= __('add_new_success', null, 'company_jobs');
					$form = new JobsFForm();
					$this->data['majors']	= array();
					$this->data['cities']	= array();
					$this->data['levels']	= array();
					$this->data['child']['languages']	= array();
					$this->data['child']['language_list']	= array();
				}else{
					$this->data['result']	= sprintf(__('add_new_error', null, 'table_list'), sfConfig::get('app_email_admin')) ;
				}
					
			}
		}
		$arrTmp = array();
		foreach($this->data['majors'] as $val){
			$arrTmp[$val] = 'checked';
		}
		$this->data['majors']	= $arrTmp;
		
		$arrTmp = array();
		foreach($this->data['cities'] as $val){
			$arrTmp[$val] = 'checked';
		}
		$this->data['cities']	= $arrTmp;
		
		$this->setVar('form', $form);
	}
	private function addLevel($job_id){
		JobToEducationLevelsTable::deleteByJobId($job_id);
		
		foreach($this->data['levels'] as $val){
			$ptData = array(
					'job_id'				=> $job_id,
					'education_level_id'	=> $val
			);
			JobToEducationLevelsTable::insert($ptData);
				
		}
	}
	private function addCity($job_id){
		JobToLocationCitiesTable::deleteByJobId($job_id);
		
		foreach($this->data['cities'] as $val){
			$ptData = array(
					'job_id'		=> $job_id,
					'location_city_id'	=> $val
			);
			JobToLocationCitiesTable::insert($ptData);
				
		}
	}
	private function addLanguage($job_id = 0){
		$job_id = intval($job_id);
		if($job_id <=0){
			return;
		}
		JobToForeignLanguagesTable::deleteByJobId($job_id);
		for($i=0; $i<count($this->data['child']['languages']);$i++){
			$work = $this->data['child']['languages'][$i];
			$workDes = $this->data['child']['language_des'][$i];
			$arr = explode(';', $work);
			if(count($arr) >=2){
				JobToForeignLanguagesTable::insert(array(
				'job_id'			=> $job_id,
				'foreign_language_id'	=> $arr[0],
				'foreign_language_level_id'		=> $arr[1],
				'description'			=> $workDes
				));
			}
		}
	}
	private function addMajor($job_id){
		JobToMajorsTable::deleteByJobId($job_id);
			
		foreach($this->data['majors'] as $val){
			$ptData = array(
					'job_id'		=> $job_id,
					'major_id'	=> $val
			);
			JobToMajorsTable::insert($ptData);
		
		}
	}
	/*
	 * EDIT
	*/
	private function editRecord(){
		$this->data['navigation'].=' › '. __('nav_edit',null,'table_list');
		$this->data['action'].='&id='.$this->data['id'];
		$this->data['button_action'] = __('button_edit',null,'table_list');
		$this->setTemplate('jobsForm');

		$this->data['list']['majors']		= MasterMajorCatesTable::getDataRecursive();
		$this->data['list']['cities']		= LocationCitiesTable::getDataList();
		$this->data['list']['levels']		= MasterEducationLevelsTable::getDataList();
		
		$form = new JobsFForm();
		if($this->data['is_post']){
			if(intval($this->data['rdSalary']) == 1){
				$this->formData['salary_from'] = '0';
				$this->formData['salary_to'] = '0';
			}elseif(intval($this->data['rdSalary']) == 2){
				$this->formData['salary_payed'] = '0';
			}else {
				$this->formData['salary_from'] = '0';
				$this->formData['salary_to'] = '0';
				$this->formData['salary_payed'] = '0';
			}
			$form->bind($this->formData);
			
			if($form->isValid()){
				$this->formData['id'] = $this->data['id'];
				if(JobsTable::update($this->formData)){
					$this->data['result']	= __('edit_success', null, 'company_jobs');
					$this->addMajor($this->data['id']);
					$this->addCity($this->data['id']);
					$this->addLevel($this->data['id']);
					
					$this->addLanguage($this->data['id']);
					$this->data['child']['languages'] = array();
					$this->data['child']['language_des'] = array();
					$this->data['child']['languages'] = JobToForeignLanguagesTable::getListByJobId($this->data['id']);
					foreach($this->data['child']['languages'] as $val){
						$this->data['child']['language_des'][] = $val['description'];
					}
				}else{
					$this->data['result']	= sprintf(__('edit_error', null, 'table_list'), sfConfig::get('app_email_admin')) ;
				}
			}
			$arrTmp = array();
			foreach($this->data['majors'] as $val){
				$arrTmp[$val] = 'checked';
			}
			$this->data['majors']	= $arrTmp;
			
			$arrTmp = array();
			foreach($this->data['cities'] as $val){
				$arrTmp[$val] = 'checked';
			}
			$this->data['cities']	= $arrTmp;
			
			$arrTmp = array();
			foreach($this->data['levels'] as $val){
				$arrTmp[$val] = 'checked';
			}
			$this->data['levels']	= $arrTmp;
			
		}else{
			$data = JobsTable::getDataByIdAdmin($this->data['id']);
			if(count($data)<0){
				$this->redirect($this->data['redirect']);
			}else{
				$this->formData = array(
						'company_id'		=> $data['company_id'],
						'position_id'		=> $data['position_id'],
						'title'				=> $data['title'],
						'short_description'	=> $data['short_description'],
						'description'		=> $data['description'],
						'date_from'					=> DateHelper::dateFormatFree($data['date_from'], 'Y-m-d'),
						'date_to'					=> DateHelper::dateFormatFree($data['date_to'], 'Y-m-d'),
						'number_recruitment'		=> $data['number_recruitment'],
						'number_recruited'			=> $data['number_recruited'],
						'require_year_experience'	=> $data['require_year_experience'],
						'require_description'		=> $data['require_description'],
						
						'require_gender'		=> $data['require_gender'],
						'probation_time'		=> $data['probation_time'],
						'probation_salary'		=> $data['probation_salary'],
						'benefit'				=> $data['benefit'],
						'major_text'			=> $data['major_text'],
						
						'salary_payed'		=> $data['salary_payed'],
						'salary_from'		=> $data['salary_from'],
						'salary_to'			=> $data['salary_to'],
						'age_from'			=> $data['age_from'],
						'age_to'			=> $data['age_to'],
						'is_recruit'		=> $data['is_recruit'],
						
						'is_enabled'		=> $data['is_enabled'],
						'_csrf_token'=> $form->getCSRFToken()
				);
				$form->bind($this->formData);
				$this->data['majors'] = JobToMajorsTable::getListByJobId($this->data['id']);
				$arrTmp = array();
				foreach($this->data['majors'] as $val){
					$arrTmp[$val['major_id']] = 'checked';
				}
				$this->data['majors'] = $arrTmp;
				
				$this->data['cities'] = JobToLocationCitiesTable::getListByJobId($this->data['id']);
				$arrTmp = array();
				foreach($this->data['cities'] as $val){
					$arrTmp[$val['location_city_id']] = 'checked';
				}
				$this->data['cities'] = $arrTmp;
				
				$this->data['levels'] = JobToEducationLevelsTable::getListByJobId($this->data['id']);
				$arrTmp = array();
				foreach($this->data['levels'] as $val){
					$arrTmp[$val['education_level_id']] = 'checked';
				}
				$this->data['levels'] = $arrTmp;
				
				if(intval($data['salary_payed'])!=0){
					$this->data['rdSalary']	= '1';
				}elseif(intval($data['salary_from'])!=0 || intval($data['salary_to'])!=0){
					$this->data['rdSalary']	= '2';
				}else{
					$this->data['rdSalary']	= '3';
				}
				
				$this->data['child']['languages'] = array();
				$this->data['child']['language_des'] = array();
				$this->data['child']['languages'] = JobToForeignLanguagesTable::getListByJobId($this->data['id']);
				foreach($this->data['child']['languages'] as $val){
					$this->data['child']['language_des'][] = $val['description'];
				}
			}
		}
		$this->setVar('form', $form);
	}
	#ACTION FOR EACH ROW
	private function setAction(){
		$action = array();
		$action[]=array(
				'href'	=>	'?action_name='.ADMIN_DEFINED::EDIT_RECORD.'&id=%s',
				'text'	=>	__('action_edit',null,'table_list'),
				'title' =>	__('action_edit',null,'table_list'),
		);
	
		$this->data['action_list']	= $action;
	}
}